﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace PolePosition.ProjectCommon
{
    public class DataTypes
    {
        public static T TryParse<T>(object source) where T:new()
        {
            T _T;
            _T = default(T);

            try
            {
                // implicit conversion
                _T = (T)source;
            }
            catch (Exception)
            {
                try
                {
                    // string conversion
                    Type _Type;
                    _Type = _T.GetType();
                    _T = (T)_Type.InvokeMember("Parse", System.Reflection.BindingFlags.InvokeMethod, null, source.ToString(), null);
                }
                catch (Exception)
                {
                    // do nothing
                }
            }
            return _T;
        }
    }
}
